package com.saret.crawler.type;

import java.util.ArrayList;
import java.util.List;

/**
 * User: biniam.gebremichael
 * Date: 2/3/11
 */
public class GWordImpl implements GWord {
    String word;
    List<GUrl> url = new ArrayList<GUrl>();
    long occurrence;

    public GWordImpl(String word, Long occurrence) {
        this.word = word;
        this.occurrence = occurrence == null ? 0 : occurrence;
    }


    @Override
    public synchronized long increment(long v) {
        occurrence = occurrence + v;
        return occurrence;
    }


    @Override
    public synchronized long increment(GUrl gUrl) {
        this.url.add(gUrl);
        return increment(1);
    }

    @Override
    public String getWord() {
        return word;
    }

    @Override
    public List<GUrl> getUrl() {
        return url;
    }

    @Override
    public long getOccurrence() {
        return occurrence;
    }

    public String toString() {
        return occurrence + "\t" + word;
    }

    @Override
    public boolean equals(Object obj) {
        return (obj instanceof GWordImpl) && (((GWord) obj).getWord().equals(getWord()));
    }

    @Override
    public int compareTo(GWord that) {
        int occCompare = (int) (that.getOccurrence() - getOccurrence());
        if (occCompare == 0) {
            return getWord().compareTo(that.getWord());
        } else {
            return occCompare;
        }
    }


    @Override
    public int hashCode() {
        return getWord().hashCode();
    }
}
